package org.ru.mse10.cvis.web.bean.action.administration.upload;

import java.io.IOException;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.myfaces.custom.fileupload.UploadedFile;
import org.ru.mse10.cvis.upload.CSVParser;
import org.ru.mse10.cvis.upload.XMLParser;
import org.ru.mse10.cvis.web.bean.BaseUIBean;

/**
 * The Class AdminUploadAction.
 * 
 * @author Spawn
 */
@SessionScoped
@ManagedBean(name = "adminUploadAction")
public class AdminUploadAction extends BaseUIBean {

	/** The xml parser. */
	private XMLParser xmlParser;

	/** The uploaded file. */
	private UploadedFile uploadedFile;

	/** The file name. */
	private String fileName;

	/** The csv parser. */
	private CSVParser csvParser;

	/**
	 * Save.
	 */
	public void save() {
		FacesContext context = FacesContext.getCurrentInstance();
		ResourceBundle bundle = context.getApplication().getResourceBundle(
				context, "message");
		try {
			fileName = uploadedFile.getName();
			if (fileName.indexOf(".xml") != -1) {
				xmlParser.parseXmlFile(uploadedFile.getInputStream());
				/** Show success message. */
				FacesContext.getCurrentInstance().addMessage(
						"uploadForm",
						new FacesMessage(FacesMessage.SEVERITY_INFO, bundle
								.getString("correctUploadFile"), null));
			} else if (fileName.indexOf(".csv") != -1) {
				csvParser.parse(uploadedFile.getInputStream());
				/** Show success message. */
				FacesContext.getCurrentInstance().addMessage(
						"uploadForm",
						new FacesMessage(FacesMessage.SEVERITY_INFO, bundle
								.getString("correctUploadFile"), null));
			} else {
				FacesContext.getCurrentInstance().addMessage(
						"uploadForm",
						new FacesMessage(FacesMessage.SEVERITY_ERROR, bundle
								.getString("incorrectUploadFile"), null));
			}
		} catch (IOException e) {
			FacesContext.getCurrentInstance().addMessage(
					"uploadForm",
					new FacesMessage(FacesMessage.SEVERITY_ERROR, bundle
							.getString("errorUploadFile"), null));
			e.printStackTrace();
		}
	}

	/**
	 * Inits the.
	 */
	private void init() {
		xmlParser = new XMLParser();
		csvParser = new CSVParser();
	}

	/**
	 * Creates the upload action.
	 * 
	 * @return the string
	 */
	public String createUploadAction() {
		init();
		return "createUploadAction";
	}

	/**
	 * Gets the uploaded file.
	 * 
	 * @return the uploaded file
	 */
	public UploadedFile getUploadedFile() {
		return uploadedFile;
	}

	/**
	 * Sets the uploaded file.
	 * 
	 * @param uploadedFile
	 *            the new uploaded file
	 */
	public void setUploadedFile(UploadedFile uploadedFile) {
		this.uploadedFile = uploadedFile;
	}

	/**
	 * Gets the file name.
	 * 
	 * @return the file name
	 */
	public String getFileName() {
		return fileName;
	}
}
